package com.littleu.yojoincn.controller.tt;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.littleu.yojoincn.common.util.PageUtil;
import com.littleu.yojoincn.dto.reserve.ReserveCreateDeliveryDTO;
import com.littleu.yojoincn.dto.reserve.ReserveQueryDTO;
import com.littleu.yojoincn.logic.reserve.DeliveryChecker;
import com.littleu.yojoincn.model.ReserveDO;
import com.littleu.yojoincn.model.ReserveDeliveryDetailDO;
import com.littleu.yojoincn.model.ReserveDetailDO;
import com.littleu.yojoincn.service.ReserveService;
import com.littleu.yojoincn.vo.CreatedVO;
import com.littleu.yojoincn.vo.PageResponseVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Positive;
import java.util.List;

@RestController
@RequestMapping("/tt/reserve")
public class ReserveController {
    @Autowired
    private ReserveService reserveService;

    @PostMapping("/page")
    public PageResponseVO<ReserveDO> getReservePaging(@RequestBody @Validated ReserveQueryDTO dto){
        IPage<ReserveDO> paging = this.reserveService.getReserveByPage(dto.getPage(), dto.getCount(), dto);
        return PageUtil.build(paging);
    }

    @GetMapping("/detail/{id}")
    public ReserveDetailDO getReserveDetail(@PathVariable @Positive Long id){
        return this.reserveService.getDetail(id);
    }

    @GetMapping("/{id}/delivery")
    public List<ReserveDeliveryDetailDO> getReserveDelivery(@PathVariable @Positive Long id){
        return this.reserveService.getReserveDeliveryWithDetail(id);
    }

    @PostMapping("/{id}/delivery")
    public CreatedVO createDelivery(@RequestBody @Validated ReserveCreateDeliveryDTO dto, @PathVariable @Positive Long id){
        DeliveryChecker deliveryChecker = this.reserveService.isOK(dto, id);
        this.reserveService.createDelivery(deliveryChecker, dto, id);
        return new CreatedVO(1);
    }
}
